package com.mukefile.dao.impl;

import com.mukefile.dao.ManagerDao;
import com.mukefile.pojo.Manager;
import com.mukefile.pojo.User;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/5/6
 */
@Repository("managerDao")
public class ManagerDaoImpl implements ManagerDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    @Override
    public Manager selectManagerInfo(Manager manager) {
        //获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        //Sql语句
        String sql = "select * from manager where mname=? and mpwd=?";
        //创建Sql查询
        Query query = session.createSQLQuery(sql).addEntity(Manager.class)
                .setParameter(1,manager.getName()).setParameter(2,manager.getPwd());
        List list = query.list();
        if (list.isEmpty()){//如果为空
            return null;
        }
        return (Manager) list.get(0);
    }

    @Override
    public List<User> showAllUser() {
        //获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        //Sql语句
        String sql = "select * from user";
        //创建Sql查询
        Query query = session.createSQLQuery(sql).addEntity(User.class);
        List list = query.list();
        return list;
    }

    @Override
    public int updateUserPwd(Integer uid) {
        //获取对象数据
        User user = this.selectUserById(uid);
        //设置密码
        user.setPassword(new Md5Hash("123456","kenewstar").toString());
        try {
            this.hibernateTemplate.update(user);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public User selectUserById(Integer uid) {
        User user = this.hibernateTemplate.get(User.class, uid);
        return user;
    }

    @Override
    public int updateDiskAvail(Integer uid, Integer size) {
        User user = this.selectUserById(uid);
        user.setDiskSize(user.getDiskSize()+size);
        user.setDiskAvail(user.getDiskAvail()+size*1024*1024);
        try {
            this.hibernateTemplate.update(user);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public List<User> selectUserByLike(String username) {
        //获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        //Sql
        String sql = "select * from user where username like ?";
        //创建查询
        Query query = session.createSQLQuery(sql).addEntity(User.class).setParameter(1, "%" + username + "%");

        return query.list();
    }

    @Override
    public List<User> selectUserByName(String username) {
        //获取session
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        //Sql
        String sql = "select * from user where username = ?";
        //创建查询
        Query query = session.createSQLQuery(sql).addEntity(User.class).setParameter(1, username);

        return query.list();
    }

    @Override
    public int insertUser(User user) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String sql = "insert into user(username,diskRootPath) values(?,?)";
        try {
            session.createSQLQuery(sql)
                    .setParameter(1,user.getUsername())
                    .setParameter(2,user.getDiskRootPath())
                    .executeUpdate();
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public User getLastUser() {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String sql = "select * from user order by id DESC limit 1";
        Query query = session.createSQLQuery(sql).addEntity(User.class);
        return query.list().isEmpty()?null:(User)query.list().get(0);
    }

    @Override
    public Manager selectManagerByName(String name) {
        Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
        String sql = "select * from manager where mname=?";
        List list = session.createSQLQuery(sql).addEntity(Manager.class).setParameter(1, name).list();
        return list.size()>0? (Manager) list.get(0) :null;
    }

}
